import type { UploadFile } from "antd";
import { UploadBigFile, UploadFinish } from "../business/FileRequest";
const MAX_BYTE:number=1024*1024*10;
const fileList:ArrayBuffer[]=[];
export default function UploadVideo(file:UploadFile<File>){
    let fr=new FileReader();
    fr.readAsArrayBuffer(file as File)
    fr.addEventListener("load",e=>loadHandler(e,file.name as string));
}

function loadHandler(e:ProgressEvent,name:string){
    const fr:FileReader=e.currentTarget as FileReader;
    const len:number=Math.ceil((fr.result as ArrayBuffer).byteLength/MAX_BYTE);
    for(let i=0;i<len;i++){
        if(i==len-1) fileList.push((fr.result as ArrayBuffer).slice(i*MAX_BYTE));
        else fileList.push((fr.result as ArrayBuffer).slice(i*MAX_BYTE,(i+1)*MAX_BYTE))
    }
    upload(name);
}

async function upload(name:string){
    for(let i=0;i<fileList.length;i++){
       const result=await UploadBigFile(name,fileList[i])
       console.log(result);
    }
    const data= await UploadFinish({name:name.split(".")[0],extName:name.split(".")[1]})
    console.log(data)
}